
#include "PlatformProperty.h"

std::map<int32_t, int32_t> m_LFAM_SeatAdjustmentVFC;
void SeatAdjustmentChangeVFC_IdleToOther_LFAM(SignalTX iSignal, int32_t iValue) {
    int32_t ikey = static_cast<int32_t>(iSignal);
    std::map<int32_t, int32_t>::iterator it = m_LFAM_SeatAdjustmentVFC.find(ikey);
    if (!(it != m_LFAM_SeatAdjustmentVFC.end())) {
        m_LFAM_SeatAdjustmentVFC[ikey] = 0;
    }
    VHALOG.d("SeatAdjustmentChangeVFC_IdleToOther_LFAM iValue=%d,last=%d,Signal=%d,key=%d", iValue,
             m_LFAM_SeatAdjustmentVFC[ikey], iSignal, ikey);
    // ChangeFromXToOther (X = SwtHozlSts1_Idle)(X = SwtVertSts1_Idle)(X = SeatFoldRaiseReq_Idle)
    if (0 == m_LFAM_SeatAdjustmentVFC[ikey] && 0 != iValue &&
        !SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModAbdnd)) {
        SIGNAL.setCB_VFC_SeatComfortFunctions(sdb::OnOff1::On);
    }
    m_LFAM_SeatAdjustmentVFC[ikey] = iValue;
}

//[SW_FC_004_059]多功能第一排座椅腰托 前后调节
SYSTEM_READ_WRITE_PROPERTY(SEAT_LUMBAR_FORE_AFT_MOVE, int32_t) {
    builder.area(VehicleAreaSeat::ROW_1_LEFT)
        .initValue([]() { return toInt(SeatLumbarExtendedMode::SEAT_LUMBAR_EXTENDED_OFF); })
        .withStatusBySignals(
            []([[maybe_unused]] const PropertyStatus& lastStatus) {
                VHALOG.d(
                    "SOME ONE SET SEAT_LUMBAR_FORE_AFT_MOVE carconfig240 %d, carModeIs %d, "
                    "usgModeIs %d",
                    SIGNAL.getcarconfig240(), SIGNAL.getVehModMngtGlbSafe1CarModSts1(),
                    SIGNAL.getVehModMngtGlbSafe1UsgModSts());
                if (SIGNAL.carConfigIs(SignalRX::carconfig240,
                                       sdb::CarConfig240::_02,
                                       sdb::CarConfig240::_03,
                                       sdb::CarConfig240::_80)) {
                    if (SIGNAL.carModeIs(sdb::CarModSts1::CarModCrash,
                                         sdb::CarModSts1::CarModDyno,
                                         sdb::CarModSts1::CarModNorm) &&
                        SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModInActv,
                                         sdb::UsgModSts1::UsgModCnvinc,
                                         sdb::UsgModSts1::UsgModActv,
                                         sdb::UsgModSts1::UsgModDrvg)) {
                        VHALOG.d("SOME ONE SET SEAT_LUMBAR_FORE_AFT_MOVE Active");
                        return PropertyStatus::Active;
                    } else {
                        VHALOG.d("SOME ONE SET SEAT_LUMBAR_FORE_AFT_MOVE NotActive");
                        return PropertyStatus::NotActive;
                    }
                } else {
                    VHALOG.d("SOME ONE SET SEAT_LUMBAR_FORE_AFT_MOVE NotAvailable");
                    return PropertyStatus::NotAvailable;
                }
            },
            SignalRX::carconfig240,
            SignalRX::VehModMngtGlbSafe1UsgModSts,
            SignalRX::VehModMngtGlbSafe1CarModSts1)
        .withValueBySignals(
            []([[maybe_unused]] const int32_t& last) {
                VHALOG.d(
                    "SEAT_LUMBAR_FORE_AFT_MOVE dirver DrvrSeatActvSpplFct is %d, "
                    "DrvrSeatSwtStsDrvrSeatSwtAdjmtOfSpplFctHozlSts is %d, "
                    "DrvrSeatBtnPsd is %d",
                    SIGNAL.getDrvrSeatActvSpplFct(),
                    SIGNAL.getDrvrSeatSwtStsDrvrSeatSwtAdjmtOfSpplFctHozlSts(),
                    SIGNAL.getDrvrSeatBtnPsd());

                if(SIGNAL.getDrvrSeatBtnPsd() == sdb::Boolean::TRUE)
                {
                    sdb::SwtHozlSts1 swtHozlSts = sdb::SwtHozlSts1::Idle;
                    SIGNAL.setLumLenAdjmtRowFirstDrvr(swtHozlSts);
                    return static_cast<int32_t>(SeatLumbarExtendedMode::SEAT_LUMBAR_EXTENDED_OFF);
                }

                if(SIGNAL.getDrvrSeatActvSpplFct() == sdb::SeatActvSpplFct1::LumExtnAndLumHei)
                {
                    if (SIGNAL.getDrvrSeatSwtStsDrvrSeatSwtAdjmtOfSpplFctHozlSts() ==
                        sdb::SwtHozlSts1::Fwd) {
                        return static_cast<int32_t>(
                            SeatLumbarExtendedMode::SEAT_LUMBAR_EXTENDED_FORWARD);
                    } else if (SIGNAL.getDrvrSeatSwtStsDrvrSeatSwtAdjmtOfSpplFctHozlSts() ==
                               sdb::SwtHozlSts1::Backw) {
                        return static_cast<int32_t>(
                            SeatLumbarExtendedMode::SEAT_LUMBAR_EXTENDED_BACKWARD);
                    }
                }
                return static_cast<int32_t>(SeatLumbarExtendedMode::SEAT_LUMBAR_EXTENDED_OFF);
            },
            SignalRX::DrvrSeatActvSpplFct,
            SignalRX::DrvrSeatSwtStsDrvrSeatSwtAdjmtOfSpplFctHozlSts,
            SignalRX::DrvrSeatBtnPsd)
        .onSetProperty([](const int32_t value) {
            VHALOG.d("SEAT_LUMBAR_FORE_AFT_MOVE : setValue[%d]", value);
            
            sdb::SwtHozlSts1 swtHozlSts = sdb::SwtHozlSts1::Idle;
            if (value == toInt(SeatLumbarExtendedMode::SEAT_LUMBAR_EXTENDED_FORWARD)) {
                swtHozlSts = sdb::SwtHozlSts1::Fwd;
            } else if (value == toInt(SeatLumbarExtendedMode::SEAT_LUMBAR_EXTENDED_BACKWARD)) {
                swtHozlSts = sdb::SwtHozlSts1::Backw;
            }

            SeatAdjustmentChangeVFC_IdleToOther_LFAM(SignalTX::LumLenAdjmtRowFirstDrvr,
                                                     static_cast<int32_t>(swtHozlSts));
            SIGNAL.setLumLenAdjmtRowFirstDrvr(swtHozlSts);
        })
        .area(VehicleAreaSeat::ROW_1_RIGHT)
        .initValue([]() { return toInt(SeatLumbarExtendedMode::SEAT_LUMBAR_EXTENDED_OFF); })
        .withStatusBySignals(
            []([[maybe_unused]] const PropertyStatus& lastStatus) {
                VHALOG.d(
                    "SEAT_LUMBAR_FORE_AFT_MOVE PASS carconfig240 %d, carModeIs %d, "
                    "usgModeIs %d",
                    SIGNAL.getcarconfig240(), SIGNAL.getVehModMngtGlbSafe1CarModSts1(),
                    SIGNAL.getVehModMngtGlbSafe1UsgModSts());
                if (SIGNAL.carConfigIs(SignalRX::carconfig240,
                                       sdb::CarConfig240::_02,
                                       sdb::CarConfig240::_03)) {
                    if (SIGNAL.carModeIs(sdb::CarModSts1::CarModCrash,
                                         sdb::CarModSts1::CarModDyno,
                                         sdb::CarModSts1::CarModNorm) &&
                        SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModInActv,
                                         sdb::UsgModSts1::UsgModCnvinc,
                                         sdb::UsgModSts1::UsgModActv,
                                         sdb::UsgModSts1::UsgModDrvg)) {
                        VHALOG.d("SOME ONE SET SEAT_LUMBAR_FORE_AFT_MOVE PASS Active");
                        return PropertyStatus::Active;
                    } else {
                        VHALOG.d("SOME ONE SET SEAT_LUMBAR_FORE_AFT_MOVE PASS NotActive");
                        return PropertyStatus::NotActive;
                    }
                } else {
                    VHALOG.d("SOME ONE SET SEAT_LUMBAR_FORE_AFT_MOVE PASS NotAvailable");
                    return PropertyStatus::NotAvailable;
                }
            },
            SignalRX::carconfig240,
            SignalRX::VehModMngtGlbSafe1UsgModSts,
            SignalRX::VehModMngtGlbSafe1CarModSts1)
        .withValueBySignals(
            []([[maybe_unused]] const int32_t& last) {
                VHALOG.d(
                    "SOME ONE GET SEAT_LUMBAR_FORE_AFT_MOVE PassSeatActvSpplFct is %d, "
                    "PassSeatSwtSts2PassSeatSwtAdjmtOfSpplFctHozlSts is %d, "
                    "PassSeatBtnPsd is %d",
                    SIGNAL.getPassSeatActvSpplFct(),
                    SIGNAL.getPassSeatSwtSts2PassSeatSwtAdjmtOfSpplFctHozlSts(),
                    SIGNAL.getPassSeatBtnPsd());

                if(SIGNAL.getPassSeatBtnPsd() == sdb::Boolean::TRUE)
                {
                    sdb::SwtHozlSts1 swtHozlSts = sdb::SwtHozlSts1::Idle;
                    SIGNAL.setLumLenAdjmtRowFirstPass(swtHozlSts);
                    return static_cast<int32_t>(SeatLumbarExtendedMode::SEAT_LUMBAR_EXTENDED_OFF);
                }

                if(SIGNAL.getPassSeatActvSpplFct() == sdb::SeatActvSpplFct1::LumExtnAndLumHei)
                {
                    if (SIGNAL.getPassSeatSwtSts2PassSeatSwtAdjmtOfSpplFctHozlSts() ==
                        sdb::SwtHozlSts1::Fwd) {
                        return static_cast<int32_t>(
                            SeatLumbarExtendedMode::SEAT_LUMBAR_EXTENDED_FORWARD);
                    } else if (SIGNAL.getPassSeatSwtSts2PassSeatSwtAdjmtOfSpplFctHozlSts() ==
                               sdb::SwtHozlSts1::Backw) {
                        return static_cast<int32_t>(
                            SeatLumbarExtendedMode::SEAT_LUMBAR_EXTENDED_BACKWARD);
                    }
                }
                return static_cast<int32_t>(SeatLumbarExtendedMode::SEAT_LUMBAR_EXTENDED_OFF);
            },
            SignalRX::PassSeatActvSpplFct,
            SignalRX::PassSeatSwtSts2PassSeatSwtAdjmtOfSpplFctHozlSts,
            SignalRX::PassSeatBtnPsd)
        .onSetProperty([](const int32_t value) {
            VHALOG.d("SEAT_LUMBAR_FORE_AFT_MOVE PASS: setValue[%d]", value);

            sdb::SwtHozlSts1 swtHozlSts = sdb::SwtHozlSts1::Idle;
            if (value == toInt(SeatLumbarExtendedMode::SEAT_LUMBAR_EXTENDED_FORWARD)) {
                swtHozlSts = sdb::SwtHozlSts1::Fwd;
            } else if (value == toInt(SeatLumbarExtendedMode::SEAT_LUMBAR_EXTENDED_BACKWARD)) {
                swtHozlSts = sdb::SwtHozlSts1::Backw;
            }
            SeatAdjustmentChangeVFC_IdleToOther_LFAM(SignalTX::LumLenAdjmtRowFirstPass,
                                                     static_cast<int32_t>(swtHozlSts));
            SIGNAL.setLumLenAdjmtRowFirstPass(swtHozlSts);
        });
}
